package com.hospital.hospitalserver.service;

import com.hospital.hospitalserver.entity.DepartmentInfo;
import com.hospital.hospitalserver.entity.Vo.DepartmentStatusData;
import com.hospital.hospitalserver.mapper.DepartmentMapper;
import com.hospital.hospitalserver.util.Pager;
import com.hospital.hospitalserver.util.PagerData;
import com.hospital.hospitalserver.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 科室接口方法实现类
 * */
@Service
@RequiredArgsConstructor
public class DepartmentService {

    private final DepartmentMapper departmentMapper;

    /**
     * 添加科室
     * */
    public Response addDepartment(DepartmentInfo departmentInfo){
        Integer count = departmentMapper.addDepartment(departmentInfo);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 编辑科室
     * */
    public Response editDepartment(DepartmentInfo departmentInfo){
        Integer count = departmentMapper.editDepartment(departmentInfo);
        if(count>0){
            return Response.success();
        }else{
            return Response.error();
        }
    }

    /**
     * 统计五种科室类型数量
     * @return 包含五种科室类型数量的Map
     */
    private DepartmentStatusData getDepartmentCategoryStats(){
        Map<String, Object> stats = departmentMapper.getDepartmentCategoryStats();
        DepartmentStatusData dsd = new DepartmentStatusData();
        dsd.setClinicalCount(convertToInteger(stats.get("clinicalCount")));
        dsd.setTechnicalCount(convertToInteger(stats.get("technicalCount")));
        dsd.setAdministrativeCount(convertToInteger(stats.get("administrativeCount")));
        dsd.setFinanceCount(convertToInteger(stats.get("financeCount")));
        dsd.setPharmacyCount(convertToInteger(stats.get("pharmacyCount")));

        return dsd;
    }

    /**
     * 安全类型转换方法
     */
    private Integer convertToInteger(Object value) {
        if (value == null) {
            return 0;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof BigDecimal) {
            return ((BigDecimal) value).intValue();
        }
        if (value instanceof Long) {
            return ((Long) value).intValue();
        }
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        return 0;
    }

    /**
     * 科室管理分页功能
     * 根据检索条件查询科室数据条数
     * */
    private Integer getDepartmentCountsBySearch(Pager pager){
        return departmentMapper.getDepartmentCountsBySearch(pager);
    }

    /**
     * 计算分页
     * */
    private Integer getDepartmentPageTotalBySearch(Pager pager){
        Integer depTotalCounts = getDepartmentCountsBySearch(pager);
        Integer pageTotal = 1; //设置初始页码为1
        //数据总条数除以pageSize每页显示条数，如取余数求模，如果有余数，页码+1
        //如果可以整除，则页码等于数据总数除以pageSize
        pageTotal = depTotalCounts/pager.getPageSize();
        if(depTotalCounts%pager.getPageSize()>0){
            pageTotal +=1;
        }
        return pageTotal;
    }

    /**
     * 根据检索条件查询分页数据
     * */
    private List<DepartmentInfo> getDepartmentPagersBySearch(Pager pager){
        Integer currentPage = pager.getCurrentPage();
        Integer startIndex = (currentPage - 1)*pager.getPageSize();
        pager.setStartIndex(startIndex);
        return departmentMapper.getDepartmentMapperDataBySearch(pager);
    }

    /**
     * 装载数据通过API发送给前端
     * */
    public Response getDepartmentPagerDataBySearch(Pager pager){
        PagerData pagerData = new PagerData();
        pagerData.setTotal(getDepartmentCountsBySearch(pager));
        pagerData.setPageTotal(getDepartmentPageTotalBySearch(pager));
        pagerData.setData(getDepartmentPagersBySearch(pager));
        pagerData.setDepartmentStatus(getDepartmentCategoryStats());
        return Response.success(pagerData);
    }

    /**
     * 查询科室列表数据
     * 提供给医生、护士等管理页面选择
     * */
    public Response getDepartmentList(){
        return Response.success(departmentMapper.getDepartmentList());
    }
}
